local M = class("ruleCheck")

function M:ctor(_, arg)
end

function M:getValue(cards)
    local values = {}
    for key, var in pairs(cards) do
        local cardstr =  string.format("%#x",var)
	    local value = math.floor(cardstr%0x10)
        table.insert(values,value)
    end
    return values
end

function M:checkNiu(cards)
    local nums = self:getValue(cards)
    local val = {}
    for key, var in pairs(nums) do
        local num 
        if var > 10 then
            num = 10
        else
            num = var
        end
        table.insert(val,num)
    end
    
    local pok = cards
    table.sort(val,function (a,b) return a > b end)
    local tenNum = 0
    local Cardall = 0
    local cow = 0
    local isWuxiao = true
    local isWuhua = true
    local zhaNum = 0
    for key, var in pairs(nums) do
        if var > 10 then
            tenNum = tenNum + 1
        end
        if var >= 5 then
            isWuxiao = false
        end
        if var <= 10 then
            isWuhua = false
        end
        Cardall = (var > 10 and 10 or var) + Cardall
    end
    if isWuxiao == true and Cardall <= 10 then
        return 14
    end
    
    for key, var in pairs(nums) do
        if zhaNum < 4 then
            zhaNum = 0
            for k, v in pairs(nums) do
                if var == v then
                    zhaNum = zhaNum + 1
                end
            end
        else
            return 13
        end
    end
    if isWuhua == true then
        return 12
    end
    local checkedPoker = {}

    if tenNum == 0 or tenNum == 1 then
        for var = 1, 4 do
            for v = var + 1, 5 do
                if ((Cardall - val[var] - val[v])%10 == 0) then
                    local num = (val[var]+val[v])%10
                    checkedPoker = {}
                    for vr = 1, 5 do
                        if vr ~= var and vr ~= v then
                            table.insert(checkedPoker,pok[vr])        
                        end 
                    end
                    if num == 0 then
                        num = 10
                    end
                    if cow < num then
                        cow = num
                    end
                end
            end
        end
    elseif tenNum == 2 then
        local num = val[3] + val[4] +val[5] 
        if num%10 == 0 then
            cow = 10
            checkedPoker = {}
            table.insert(checkedPoker,pok[3])  
            table.insert(checkedPoker,pok[4])  
            table.insert(checkedPoker,pok[5])  
        else
            for var = 3, 5 do
                if (num - val[var])%10 == 0 then
                    local dt = val[var]
                    if cow < dt then
                        checkedPoker = {}
                        for v = 3, 5 do
                            if v ~= var then
                                table.insert(checkedPoker,pok[v])                     
                            end
                        end
                        table.insert(checkedPoker,pok[1])
                        cow = dt
                    end
                end
            end
        end
        
    elseif tenNum == 3 then
        cow = (val[4] +val[5] )%10
        for var = 1, 3 do
            table.insert(checkedPoker,pok[var])
        end
        if cow == 0 then
            cow = 10
        end
    elseif tenNum == 4 then
        cow = val[5]
        for var = 1, 3 do
            table.insert(checkedPoker,pok[var])
        end
    elseif tenNum == 5 then
        cow = 10
        for var = 1, 3 do
            table.insert(checkedPoker,pok[var])
        end
    end
    -- if tenNum > 0 and cow == 10 then
        -- return 11
    -- end
    return cow,checkedPoker
end

return M